object ScoreSystem extends App {
  case class Student(id: Int, name: String, cls: String, scores: List[Int])
  val subjects = List("数学", "语文", "英语", "物理", "化学")
  val classes = List("班级1", "班级2", "班级3", "班级4")
  val random = new scala.util.Random(123)
  val students = (for {
    cls <- classes
    i <- 0 until 30
    id = classes.indexOf(cls) * 30 + i + 1
  } yield Student(id, s"学生$id", cls, List.fill(5)(random.nextInt(51) + 50))).toList
  println("=== 学生成绩汇总 ===")
  students.foreach { s =>
    val total = s.scores.sum
    println(f"学号${s.id} ${s.name}（${s.cls}）：总分$total，平均分${total / 5.0}%.2f")
  }
  println("\n=== 班级分数段 ===")
  classes.foreach { cls =>
    val counts = students.filter(_.cls == cls).flatMap(_.scores)
      .groupBy(score =>
        if (score >= 90) "90+"
        else if (score >= 80) "80-89"
        else if (score >= 70) "70-79"
        else if (score >= 60) "60-69"
        else "60-"
      ).mapValues(_.size)
    println(s"\n$cls:")
    List("90+", "80-89", "70-79", "60-69", "60-").foreach(r =>
      println(s"$r：${counts.getOrElse(r, 0)}人")
    )
  }
  println("\n=== 优秀学生 ===")
  students.filter(s => s.scores.forall(_ >= 60) && s.scores.sum / 5.0 >= 85)
    .foreach(s => println(f"学号${s.id} ${s.name}（${s.cls}），平均分${s.scores.sum / 5.0}%.2f"))
  println("\n=== 科目排名 ===")
  subjects.zipWithIndex.foreach { case (subj, idx) =>
    classes.map(cls => (cls, students.filter(_.cls == cls).map(_.scores(idx)).sum / 30.0))
      .sortBy(-_._2).zipWithIndex.foreach { case ((cls, avg), i) =>
      if (i == 0) println(s"\n$subj:")
      println(f"第${i + 1}名：$cls ${avg}%.2f")
    }
  }
}
